package com.wechat.dev.netty;

import java.time.LocalTime;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;

public class SxHandler extends SimpleChannelInboundHandler<TextWebSocketFrame>{
	
	private static ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
		// TODO Auto-generated method stub
		String text = msg.text();
		clients.writeAndFlush(new TextWebSocketFrame("[服务器在：]" + LocalTime.now() + "发送了：" + text));
		System.out.println("接受到的消息 ： " + text);
	}

	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		clients.add(ctx.channel());
	}

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		System.out.println("客户端端口 + 长Id" + ctx.channel().id().asLongText());
		System.out.println("客户端端口 + 短Id" + ctx.channel().id().asShortText());
	}
	
	
}
